package com.google.code.dnaep.jdo;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class PolymorphicRelationshipLifecycleListenerTest {

	private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

	@Before
	public void setUp() {
		helper.setUp();
	}

	@After
	public void tearDown() {
		helper.tearDown();
	}

	@Test
	public void testObject_null() {
		TestParentObject myParent = new TestParentObject();
		myParent.myChild = new TestChildSubOne();
		
		javax.jdo.PersistenceManagerFactory pmf = PMF.get();
		javax.jdo.PersistenceManager pm = pmf.getPersistenceManager();
		
		TestParentObject myParent2;
		try {
			myParent2 = pm.makePersistent(myParent);
		} finally {
			pm.close();
		}
		
		assertNotNull(myParent2);
		assertNotNull(myParent.myChild);
		assertNotNull(myParent.myChildKey);
	}

	@Test
	public void testObject_notNull() {
		TestParentObject myParent = new TestParentObject();
		myParent.myChild = new TestChildSubOne();
		
		javax.jdo.PersistenceManagerFactory pmf = PMF.get();
		javax.jdo.PersistenceManager pm = pmf.getPersistenceManager();
		
		TestParentObject myParent2;
		try {
			myParent2 = pm.makePersistent(myParent);
		} finally {
			pm.close();
		}
		
		assertNotNull(myParent.myChild);
		assertNotNull(myParent.myChildKey);
	}

	@Test
	public void testCollection_null() {
		TestParentCollection myParent = new TestParentCollection();
		
		javax.jdo.PersistenceManagerFactory pmf = PMF.get();
		javax.jdo.PersistenceManager pm = pmf.getPersistenceManager();
		
		TestParentCollection myParent2;
		try {
			myParent2 = pm.makePersistent(myParent);
		} finally {
			pm.close();
		}
		
		assertNull(myParent.myChildren);
		assertNull(myParent.myChildKeys);
	}

	@Test
	public void testCollection_notNull() {
		TestParentCollection myParent = new TestParentCollection();
		myParent.myChildren = new ArrayList();
		myParent.myChildren.add(new TestChildSubOne());
		myParent.myChildren.add(new TestChildSubTwo());
		
		javax.jdo.PersistenceManagerFactory pmf = PMF.get();
		javax.jdo.PersistenceManager pm = pmf.getPersistenceManager();
		
		TestParentCollection myParent2;
		try {
			myParent2 = pm.makePersistent(myParent);
		} finally {
			pm.close();
		}
		
		assertNotNull(myParent.myChildren);
		assertNotNull(myParent.myChildKeys);

		pm = pmf.getPersistenceManager();
		TestParentCollection myParent3;
		try {
			myParent3 = pm.getObjectById(TestParentCollection.class, myParent2.objectId);
		} finally {
			pm.close();
		}

		assertNotNull(myParent3.myChildren);
		assertNotNull(myParent3.myChildKeys);
	}

}
